package dwl.graduation.design.job;

import dwl.graduation.design.domain.SysCollect;
import dwl.graduation.design.mapper.SysCollectMapper;
import dwl.graduation.design.oshi.MyCpu;
import dwl.graduation.design.oshi.SystemHardwareInfo;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.Date;

@DisallowConcurrentExecution
@Component
public class CollectSysJob extends QuartzJobBean {

    @Autowired
    private SysCollectMapper sysCollectMapper;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try{
            SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo();
            //采集hardware硬件信息
            systemHardwareInfo.copyTo();
            SysCollect sysCollect = new SysCollect();
            sysCollect.setCpuTotal(systemHardwareInfo.getCpu().getTotal());
            sysCollect.setCpuSys(systemHardwareInfo.getCpu().getSys());
            sysCollect.setCpuUsed(systemHardwareInfo.getCpu().getUsed());
            sysCollect.setMemTotal(systemHardwareInfo.getMem().getTotal());
            sysCollect.setMemUsage(systemHardwareInfo.getMem().getUsage());
            sysCollect.setCreateTime(new Date());
            sysCollectMapper.insertSelective(sysCollect);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
